﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using DataLayer.Controller;


public partial class provider_OrderWork :  cGlobal
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Request.QueryString["NIT"] != null)
            {
                this.hdnIdProveedor.Value = ProviderController.getIdProveedor(Request.QueryString["NIT"].ToString());
                this.loadGrid();
            }                        
        }
    }

    private void loadGrid()
    {
        DataTable dtt = OTController.GetAllbyProveedor(this.hdnIdProveedor.Value);
        grvOT.DataSource = dtt;
        grvOT.DataBind();

        //Se guarda en session la tabla de los resultados, para poder filtrar
        Session["VIEWOT"] = dtt;
    }

    protected void grvOT_RowDataBound(object sender, Obout.Grid.GridRowEventArgs e)
    {
        if (e.Row.RowType == Obout.Grid.GridRowType.DataRow)
        {
            HyperLink lnkVerOT = (HyperLink)e.Row.Cells[6].Controls[0].Controls[0].Controls[0].Controls[0].Controls[1];
            lnkVerOT.NavigateUrl = "Javascript:openShadowbox('" + ConfigurationManager.AppSettings["urlPortal"] + "/provider/OrderDetail.aspx?OT=" + DataBinder.Eval(e.Row.DataItem, "ID_OT_SDFC").ToString().Trim() + "','iframe','Detalle Orden de Trabajo',550,450);";

            string estado = DataBinder.Eval(e.Row.DataItem, "ESTADO").ToString().Trim();

            HyperLink lnkCargarActa = (HyperLink)e.Row.Cells[7].Controls[0].Controls[0].Controls[0].Controls[0].Controls[1];

            if (estado.ToUpper().Equals("CERRADO"))
                lnkCargarActa.NavigateUrl = "Javascript:alert('Esta orden de trabajo ya está cerrada y no se le puede subir acta')";
            else
                lnkCargarActa.NavigateUrl = "Javascript:openShadowbox('" + ConfigurationManager.AppSettings["urlPortal"] + "/provider/UploadActa.aspx?OT=" + DataBinder.Eval(e.Row.DataItem, "ID_OT_SDFC").ToString().Trim() + "&name=" + DataBinder.Eval(e.Row.DataItem, "CODIGO_ORDEN").ToString().Trim() + "','iframe','Subir acta',550,250);";


            
            e.Row.Cells[2].ForeColor = System.Drawing.Color.Gray;
            if (estado == "Pendiente")
                e.Row.Cells[2].ForeColor = System.Drawing.Color.FromArgb(00, 33, 99);
            else if (estado == "Cerrado")
                e.Row.Cells[2].ForeColor = System.Drawing.Color.Red;
        }
    }


    #region "Filtros de la grilla"

    protected void btnBuscarGrilla_Click(object sender, ImageClickEventArgs e)
    {
        if (string.IsNullOrEmpty(this.txtContiene.Text.Trim()))
            return;

        if (Session["VIEWOT"] != null)
        {
            DataTable dtt = (DataTable)Session["VIEWOT"];
            string columna = this.hdnColumnaSeleccionada.Value.ToString();

            DataView dw = dtt.DefaultView;
            dw.RowFilter = columna + " LIKE '%" + this.txtContiene.Text.Trim() + "%'";
            dtt = dw.ToTable();

            this.grvOT.DataSource = dtt;
            this.grvOT.DataBind();
            Session["VIEWOT"] = dtt;

            this.txtContiene.Text = string.Empty;
        }
    }

    protected void lnkAscendente_Click(object sender, EventArgs e)
    {
        if (Session["VIEWOT"] != null)
        {
            DataTable dtt = (DataTable)Session["VIEWOT"];
            string columna = this.hdnColumnaSeleccionada.Value.ToString();

            DataView dw = dtt.DefaultView;
            dw.Sort = columna + " ASC";
            dtt = dw.ToTable();

            this.grvOT.DataSource = dtt;
            this.grvOT.DataBind();
            Session["VIEWOT"] = dtt;
        }
    }

    protected void lnkDescendente_Click(object sender, EventArgs e)
    {
        if (Session["VIEWOT"] != null)
        {
            DataTable dtt = (DataTable)Session["VIEWOT"];
            string columna = this.hdnColumnaSeleccionada.Value.ToString();

            DataView dw = dtt.DefaultView;
            dw.Sort = columna + " DESC";
            dtt = dw.ToTable();

            this.grvOT.DataSource = dtt;
            this.grvOT.DataBind();
            Session["VIEWOT"] = dtt;
        }
    }

    protected void lnkSinFiltro_Click(object sender, EventArgs e)
    {
        this.loadGrid();
    }

    #endregion


}
